#include <stdio.h>
#include <pthread.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <semaphore.h>
//互斥
//gcc -lpthread
char buf[32]={0};
sem_t sem;
int a[10]={0,1,2,3,4,5,6,7,8,9};
pthread_mutex_t lock;

void* handler_swap(void* arg){
    while(1){
        pthread_mutex_lock(&lock);
        int t=-1;
        for(int i =0;i < 5;i++){
            t=a[i];
            a[i]=a[9-i];
            a[9-i]=t;
        }
        pthread_mutex_unlock(&lock);
    }
    
    pthread_exit(NULL);
}

void* handler_print(void* arg){
    
    while(1){
        pthread_mutex_lock(&lock);
        for(int i =0;i < 10;i++){
            printf("%d  ",a[i]);
        }
        printf("\n");
        pthread_mutex_unlock(&lock);
        sleep(1);
        
        
        
    }
    
    
    
    pthread_exit(NULL);
}


int main(void){

    pthread_t tid1=-1;
    pthread_t tid2=-1;

    if(pthread_mutex_init(&lock,NULL) != 0){
        perror("pthread_mutex_init err");
        return -1;
    }
    
    if(pthread_create(&tid1,NULL,handler_swap,NULL) != 0){
        perror("swap_create err");
        return -1;
    }

    if(pthread_create(&tid2,NULL,handler_print,NULL) != 0){
        perror("print_create err");
        return -1;
    }



    if(pthread_join(tid1,NULL)){
        printf("error\n");
    }
    if(pthread_join(tid2,NULL)){
        printf("error\n");
    }

    return 0;
}